﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace formats
{
    public class lmx : placemarkAbstract
    {
     public lmx()
        {
            xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

            content = "<lm:lmx xmlns:lm=\"http://www.nokia.com/schemas/location/landmarks/1/0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.nokia.com/schemas/location/landmarks/1/0/ lmx.xsd\">";
            contentF = "</lm:lmx>";

            document = "<lm:landmarkCollection>";
            documentF = "</lm:landmarkCollection>";

            placemark = "<lm:landmark>";
            placemarkF = "</lm:landmark>";

            name = "<lm:name>";
            nameF = "</lm:name>";

            description = "<lm:description>";
            descriptionF = "</lm:description>";

            position = "<lm:coordinates>";
            positionF = "</lm:coordinates>";

            pLat = "<lm:latitude>";
            pLatF = "</lm:latitude>";

            pLong = "<lm:longitude>";
            pLongF = "</lm:longitude>";

            pAlt = "<lm:altitude>";
            pAltF = "</lm:altitude>";

            data = new List<plm>();
        }

     public override List<plm> read(System.IO.Stream streamIn)
     {
         plm input;
         Boolean documentHasName = false;
         String firstHeader = "";

         if (streamIn.Length < 10) return null;

         if (readHeader(streamIn).Equals(xml))    //xml version correct
         {
             if (readHeader(streamIn).Equals(content))    //content header is correct
             {
                 if (readHeader(streamIn).Equals(document))   //corect document header
                 {
                     if ((firstHeader = readHeader(streamIn)).Equals(name))
                     {
                         readContent(streamIn);
                         readHeader(streamIn);
                         documentHasName = true;
                     }
                     while (((!documentHasName) && (firstHeader.Equals(placemark))) || (readHeader(streamIn).Equals(placemark)))
                     {
                         documentHasName = true;
                         input = new plm();
                         String inputPlacemark = " ";
                         while (!inputPlacemark.EndsWith(placemarkF))
                         {
                             inputPlacemark += (char)streamIn.ReadByte();
                         };

                         if (inputPlacemark.Contains(name))
                             input.name = inputPlacemark.Substring(inputPlacemark.IndexOf(name) + name.Length, (inputPlacemark.IndexOf(nameF) - (inputPlacemark.IndexOf(name) + name.Length)));
                         else input.name = " ";

                         if (inputPlacemark.Contains(description))
                             input.description = inputPlacemark.Substring(inputPlacemark.IndexOf(description) + description.Length, (inputPlacemark.IndexOf(descriptionF) - (inputPlacemark.IndexOf(description) + description.Length)));
                         else input.description = " ";

                         if (inputPlacemark.Contains(pLong))
                             input.longitude = inputPlacemark.Substring(inputPlacemark.IndexOf(pLong) + pLong.Length, (inputPlacemark.IndexOf(pLongF) - (inputPlacemark.IndexOf(pLong) + pLong.Length)));
                         else input.longitude = "0";

                         if (inputPlacemark.Contains(pLat))
                             input.latitude = inputPlacemark.Substring(inputPlacemark.IndexOf(pLat) + pLat.Length, (inputPlacemark.IndexOf(pLatF) - (inputPlacemark.IndexOf(pLat) + pLat.Length)));
                         else input.latitude = "0";

                         if (inputPlacemark.Contains(pAlt))
                             input.altitude = inputPlacemark.Substring(inputPlacemark.IndexOf(pAlt) + pAlt.Length, (inputPlacemark.IndexOf(pAltF) - (inputPlacemark.IndexOf(pAlt) + pAlt.Length)));
                         else input.altitude = "0";

                         data.Add(input);
                     }
                 }
                 else return null;
             }
             else return null;
         }
         else return null;

         return data;
     }

     private String readHeader(System.IO.Stream streamIn)
     {
         String header = " ";
         Char temp;
         do
         {
             temp = (char)streamIn.ReadByte();
             header += temp;
         } while (temp != '>');
         return header.Trim();
     }

     private String readContent(System.IO.Stream streamIn)
     {
         String content = " ";
         Char c;
         while ((c = (char)streamIn.ReadByte()) != '<')
         {
             content += c;
         }
         streamIn.Position = streamIn.Position - 1;
         if (content == " ") return content;
         else return content.Trim();
     }

     public override Boolean write(System.IO.Stream streamOut, List<plm> data)
     {
         System.IO.StreamWriter writer = new System.IO.StreamWriter(streamOut);
         if (!streamOut.CanWrite) return false;
         writer.WriteLine(xml);
         writer.WriteLine(content);
         writer.WriteLine(document);
         foreach (plm item in data)
         {
             writer.WriteLine(placemark);
             writer.WriteLine(name + item.name + nameF);
             writer.WriteLine(description + item.description + descriptionF);
             writer.WriteLine(position);
             writer.WriteLine(pLat + item.latitude+ pLatF);
             writer.WriteLine(pLong + item.longitude + pLongF);
             writer.WriteLine(pAlt + item.altitude + pAltF);
             writer.WriteLine(positionF);
             writer.WriteLine(placemarkF);
         }
         writer.WriteLine(documentF);
         writer.WriteLine(contentF);
         writer.Close();
         return true;
     }
    }
}